
%macro get_amendments;

			
	proc printto new log = "&logdir./get_amendments.txt";
	run;
		/****************************************
		*										*
		*			Start PROC SQL				*
		*										*
		****************************************/
		proc sql noprint;
		[redacted]
			(
				select distinct
					[redacted] as firm_id,
					floor(case when [redacted] = 296 then -[redacted] else 0 end) as ertc_amendment,
					floor(case when [redacted] = 299 then -[redacted] else 0 end) as ffcra_amendment,
					[redacted] as tp,
					[redacted] as ext_year,
					[redacted] as ext_month
				into #t1
				from [redacted]
				where [redacted] between 202006 and 202112
					and [redacted] = 1
					and [redacted] = 766
					and [redacted] in(296,299)
					and [redacted] < 0
					and [redacted] = 54
				
				
				select
					sum(ertc_amendment) as ertc_amendment,
					sum(ffcra_amendment) as ffcra_amendment,
					firm_id,
					tp,
					ext_month,
					ext_year
				from #t1
				group by firm_id,
					tp,
					ext_month,
					ext_year
				order by firm_id,
					tp,
					ext_month,
					ext_year
				
					
		);
	disconnect from iq;
	quit;
	
	proc means data = rlib.amendments nway noprint;
		class firm_id tp;
		output out = rlib.amendments_agg(drop=_type_ _freq_)
			sum(ertc_amendment ffcra_amendment) = ertc_amendment ffcra_amendment; 
	run;
	
	proc printto;
	run;

%mend;
